﻿namespace Web.Control.Member.User
{
	using System;
	using System.Data;
	using System.Drawing;
	using System.Web;
	using System.Web.Security;
	using System.Web.UI.WebControls;
	using System.Web.UI.HtmlControls;
	using System.Text.RegularExpressions;
    using Lx.Web.Common.UI;
    using Lx.Web.Common.ControlsUI;
	using System.Collections;
    using System.Collections.Generic;
    using BLF.WWW_User.Users;
    using Model.WWW_User.User;

    public class Register : BaseControl
	{
		protected Lx.Web.Common.UI.DatePanel BOD;
        /// <summary>
        /// 推荐人编号
        /// </summary>
        protected string memberno="";
        /// <summary>
        /// 推荐人信息
        /// </summary>
        protected UserSummaryInfoModel us=null;
        protected string Emailname = "";
        /// <summary>
        /// 注册提交后的返回信息(返回会员号表示正常)
        /// </summary>
        protected string regsave = "";
        /// <summary>
        /// 注册标记1表示注册
        /// </summary>
        protected string Flag = "0";
        
		protected override void OnLoad(EventArgs e)
		{
            if (Session["RE_ra"] == null)
            {
                Page.Session["RE_ra"] = null;
            }
            if (!string.IsNullOrEmpty(Page.Request["Email"]))
            {
                Emailname = Page.Request["Email"].ToString();
            }
            if (Page.Request["ref"] != null)
            {
                memberno = this.Page.Request["ref"].ToString();
            }
            else if (Session["ref"] != null)
            {
                memberno = Session["ref"].ToString();
            }
           if (Request["Flag"] == "1" && !string.IsNullOrEmpty(Request.Form["MotherTongue"]) && !string.IsNullOrEmpty(Request.Form["Country"])
               && !string.IsNullOrEmpty(Request.Form["MemberCrtNo"]) && !string.IsNullOrEmpty(Request.Form["Password"]) && !string.IsNullOrEmpty(Request.Form["NickName"]) && !string.IsNullOrEmpty(Request.Form["re_ra"])
               )
           {
               if (Session["RE_ra"] != null && (Session["RE_ra"].ToString() == Request.Form["re_ra"].ToString()))
               {
                   if (System.Text.RegularExpressions.Regex.IsMatch(Request.Form["MemberCrtNo"], "\\w+([-+.']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*"))
                   {
                       UserModel bum;
                       RegiUserModel user = new RegiUserModel(Request.Form["MotherTongue"], "5", "", "", 3, Request.Form["Country"]
                           , Request.Form["timezone"], Request.Form["MemberCrtNo"], Globals.Enctype(Request.Form["Password"]), Request.Form["NickName"], "", DateTime.Parse("1900-1-1"), Globals.Language, memberno);
                       regsave = BLF.WWW_User.Users.User.Register(user, out bum);
                       if (regsave.Length == 10 && bum.MemberNo != "")
                       {
                           string link = System.Configuration.ConfigurationManager.AppSettings["LoginHost"] + "/user/confirm.htm?MemberNo=" + bum.MemberNo + "&AffirmNo=" + Globals.Enctype(bum.AffirmNo);
                           string subject = Globals.ReadTextByXmlNode(FormatFileName(Server.MapPath(String.Format("/Template/emailxml/{0}/Welcome.xml", Globals.Language))), "email", "subject", "");
                           string body = Globals.ReadTextByXmlNode(FormatFileName(Server.MapPath(String.Format("/Template/emailxml/{0}/Welcome.xml", Globals.Language))), "email", "body", "");
                           MsMailSend.SendMail(subject, body.Replace("$$link$$", link).Replace("$$NickName$$", bum.Nickname), bum.MemberCrtNo, null, true);
                           Server.Transfer(string.Format("/User/registercomplete.htm?step=2&uid={0}", bum.MemberNo), true);
                           return;
                       }
                       else
                       {
                           Emailname = Request.Form["MemberCrtNo"];
                       }
                   }

               }
           }
           else
           {
               Random ra = new Random();
               int re_ra = ra.Next(9999);
               Session["RE_ra"] = (re_ra * 2 * 3).ToString();
               Response.Write("<script>var re_ra=" + re_ra + ";</script>");
           }
            if (memberno != "")
            {
                us = User.GetUserBaseProfile(memberno);
            }
			base.OnLoad (e);
		}
		
		protected override void OnInit(EventArgs e)
		{
			BOD.TextYMD = this.GetText("UR078");
			base.OnInit (e);
		}
	}
}


